

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;


/**
 * Created by John on 2016/5/6.
 * 功能：坦克1.0
 */
public class MyTank1 extends JFrame implements ActionListener
{
    MyPanel mp=null;
    MyStartPanel msp=null;
    JMenuBar jmb=null;
    JMenu jm1=null;
    JMenuItem jmi1=null;
    JMenuItem jmi2=null;
    JMenuItem jmi3=null;
    JMenuItem jmi4=null;
    public static void main(String[] args)
    {
        MyTank1 mt1=new MyTank1();
    }
    public MyTank1()
    {
        //mp=new MyPanel();
        //启动mp线程
       // Thread t=new Thread(mp);
        //t.start();
       // this.add(mp);
        //this.addKeyListener(mp);
        //创建菜单及菜单选项
        jmb=new JMenuBar();
        jm1=new JMenu("游戏（G）");
        //设置快捷键
        jm1.setMnemonic('G');
        jmi1=new JMenuItem("开始新游戏（N)");
        jmi2=new JMenuItem("退出游戏(E)");
        jmi3=new JMenuItem("存盘退出（C）");
        jmi4=new JMenuItem("继续上局游戏（S）");
        //注册监听
        jmi4.addActionListener(this);
        jmi4.setActionCommand("conGame");
        jmi3.addActionListener(this);
        jmi3.setActionCommand("saveExit");
        jmi2.addActionListener(this);
        jmi2.setActionCommand("exit");
        jmi2.setMnemonic('E');
        //对jmi1相应
        jmi1.addActionListener(this);
        jmi1.setActionCommand("newgame");
        jm1.add(jmi1);
        jm1.add(jmi2);
        jm1.add(jmi3);
        jm1.add(jmi4);
        jmb.add(jm1);

        msp=new MyStartPanel();
        Thread t=new Thread(msp);
        t.start();
        this.setJMenuBar(jmb);
        this.add(msp);
        this.setSize(600,500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //对用户不同的点击进行chuli
        if (e.getActionCommand().equals("newgame"))
        {
            mp=new MyPanel("newGame");
            //启动mp线程
             Thread t=new Thread(mp);
            t.start();
            //先删除旧的面板
            this.remove(msp);
             this.add(mp);
            this.addKeyListener(mp);
            //显示，刷新面板
            this.setVisible(true);
        }else if (e.getActionCommand().equals("exit"))
        {
            Recorder.keepRecording();
            System.exit(0);
        }else if (e.getActionCommand().equals("saveExit"))
        {
            Recorder rd=new Recorder();
            rd.setEts(mp.ets);
            rd.keepRecAndEnemyTank();
            System.exit(0);
        }else if (e.getActionCommand().equals("conGame"))
        {
            mp=new MyPanel("con");

            //启动mp线程
            Thread t=new Thread(mp);
            t.start();
            //先删除旧的面板
            this.remove(msp);
            this.add(mp);
            this.addKeyListener(mp);
            //显示，刷新面板
            this.setVisible(true);
        }
    }
}
//提示面板
class MyStartPanel extends JPanel implements Runnable
{
    int times=0;
    public void paint(Graphics g)
    {

        super.paint(g);
        g.fillRect(0,0,400,300);
        //提示信息
        if (times%2==0) {
            g.setColor(Color.yellow);
            //开关信息的字体
            Font myFont = new Font("华文新魏", Font.BOLD, 30);
            g.setFont(myFont);
            g.drawString("stage:1", 150, 150);
        }
    }

    @Override
    public void run() {
        while(true)
        {
            //休眠
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            times++;
            //重画
            this.repaint();


        }
    }
}
//我的面板1

class MyPanel extends JPanel implements KeyListener,Runnable
{


    Hero hero=null;

    Vector<EnemyTank> ets=new Vector<EnemyTank>();
    Vector<Node> nodes=new Vector<Node>();
    Vector<Bomb> bombs=new Vector<Bomb>();
    int enSize=4;

    Image image1=null;
    Image image2=null;
    Image image3=null;
    //构造函数
    public MyPanel(String flag)
    {
        File file=new File("d:\\myRecording.txt");
        if(file.exists())
            Recorder.getRecording();
        hero =new Hero(100,100);
        if (flag.equals("newGame")) {
            for (int i = 0; i < enSize; i++) {
                EnemyTank et = new EnemyTank((i + 1) * 50, 0);
                et.setDirect(1);
                et.setEts(ets);
                ets.add(et);
                Thread t = new Thread(et);
                t.start();
                Shot s = new Shot(et.x + 10, et.y + 30, 2);
                //加入给敌人
                et.ss.add(s);
                Thread t2 = new Thread(s);
                t2.start();
                //ets.add(et);

            }
        }else {

            nodes=new Recorder().getNodesAndEnNums();
            for (int i = 0; i < nodes.size(); i++) {

                Node node = nodes.get(i);
                EnemyTank et = new EnemyTank(node.x, node.y);
                et.setDirect(node.direct);
                et.setEts(ets);
                ets.add(et);
                Thread t = new Thread(et);
                t.start();
                Shot s = new Shot(et.x + 10, et.y + 30, 2);
                //加入给敌人
                et.ss.add(s);
                Thread t2 = new Thread(s);
                t2.start();
                ets.add(et);

            }
        }
        // 初始化图片
       // image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("explosion1-12.png"));
    }
    //画出提示信息
    public void showInfo(Graphics g)
    {
        this.drawTank(80,310,g,0,1);
        g.setColor(Color.black);
        g.drawString(Recorder.getEnNum()+"",100,330);
        this.drawTank(128,310,g,0,0);
        g.setColor(Color.black);
        g.drawString(Recorder.getMyLife()+"",150,330);
        //画出玩家的总成绩
        g.setColor(Color.black);
        Font f=new Font("宋体",Font.BOLD,20);
        g.setFont(f);
        g.drawString("您的总成绩",400,30);
        this.drawTank(420,60,g,0,1);
        g.setColor(Color.black);
        g.drawString(Recorder.getAllEnNum()+"",446,80);
    }
    //重写paint
    public void paint(Graphics g)
    {
        super.paint(g);
        g.fillRect(0,0,400,300);
        //画出提示信息
        this.showInfo(g);
        //画出提示坦克，不参与战斗

        if (hero.isLive)
        this.drawTank(hero.getX(),hero.getY(),g,hero.getDirect(),0);
        for (int i=0;i<this.hero.ss.size();i++)
        {   Shot myShot=hero.ss.get(i);
            if (myShot!= null && hero.s.isLive)
            {
                g.draw3DRect(myShot.x, myShot.y, 1, 1, false);
            }

            if (myShot != null && !myShot.isLive) hero.ss.remove(myShot);
        }
        //画出炸弹
        for (int i=0;bombs.size()>i;i++)
        {
            Bomb b=bombs.get(i);
            //此地还需改进爆炸效果，life 是9
            g.drawImage(image1,b.x,b.y,30,30,this);
            b.lifeDown();
            if (b.life==0)
            {
                bombs.remove(b);
            }
        }
        //画出敌人坦克
        for(int i=0;i<ets.size();i++)
        {
            EnemyTank et=ets.get(i);
            if (et .isLive) {
                this.drawTank(et.getX(), et.getY(), g, et.getDirect(), 1);
            //再画出敌人坦克子弹
                for (int j=0;j<et.ss.size();j++)
                {
                    Shot enemyShot=et.ss.get(j);
                    if (enemyShot.isLive)
                    {
                        g.draw3DRect(enemyShot.x,enemyShot.y,1,1,false);
                    }
                    else{
                        et.ss.remove(enemyShot);
                    }

                }
            }
        }
    }
    //判断我是否被击中
    public void hitMe()
    {
        for (int i=0;i<this.ets.size();i++)
        {
            EnemyTank et=ets.get(i);
            for (int j=0;j<et.ss.size();j++)
            {
                Shot enemyShot=et.ss.get(j);
                if (hero.isLive)
                {
                    this.hitTank(enemyShot,hero);
                }


            }
        }
    }
    //判断是否击中敌人坦克
    public void hitEnemy()
    {
        for (int i=0;i<hero.ss.size();i++)
        {
            Shot myShot=hero.ss.get(i);
            if (myShot.isLive=true)
            {
                for (int j=0;j<ets.size();j++)
                {
                    EnemyTank et=ets.get(j);
                    if (et.isLive)
                    {
                        if(this.hitTank(myShot,et))
                        {
                            Recorder.reduceEnNun();
                            Recorder.addEnNumRec();
                        }

                    }
                }
            }
        }
    }
    //判断子弹击中坦克的函数
    public boolean hitTank(Shot s,Tank et)
    {
        boolean b2=false;
        switch (et.direct)
        {
            case 0:
            case 1:
                if (s.x>et.x&&s.x<et.x+20&&s.y>et.y&&s.y<et.y+30)
                {
                    s.isLive=false;
                    et.isLive=false;
                    b2=true;
                    //创建一颗炸弹
                     Bomb b=new Bomb(et.x,et.y);
                    //放入Vector
                    bombs.add(b);
                }
                break;
            case 2:
            case 3:
                if (s.x>et.x&&s.x<et.x+30&&s.y>et.y&&s.y<et.y+20)
                {
                    s.isLive=false;
                    et.isLive=false;
                    b2=true;
                    //创建一颗炸弹
                    Bomb b=new Bomb(et.x,et.y);
                    //放入Vector
                    bombs.add(b);
                }
                break;
        }
        return b2;
    }
    public void drawTank(int x,int y,Graphics g,int direct,int type)
    {
        switch (type)
        {
            case 0:
                g.setColor(Color.CYAN);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;

        }
        switch (direct)
        {
            case 0://向上


                //  左边矩形
                g.fill3DRect(x,y,5,30,false);
                //右边矩形
                g.fill3DRect(x+15,y,5,30,false);
                //中间矩形
                g.fill3DRect(x+5,y+5,10,20,false);
                //圆形
                g.fillOval(x+5,y+10,10,10);
                //画出线
                g.drawLine(x+9,y+15,x+9,y);
                break;
            case 3://向右
                g.fill3DRect(x,y,30,5,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x+30,y+10);
                break;
            case 2://向左
                g.fill3DRect(x,y,30,5,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x,y+10);
                break;
            case 1:
                g.fill3DRect(x,y,5,30,false);
                //右边矩形
                g.fill3DRect(x+15,y,5,30,false);
                //中间矩形
                g.fill3DRect(x+5,y+5,10,20,false);
                //圆形
                g.fillOval(x+5,y+10,10,10);
                //画出线
                g.drawLine(x+10,y+15,x+10,y+30);
                break;



        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode()==KeyEvent.VK_W)
        {
            this.hero.setDirect(0);
            this.hero.moveUp();
        }
        else if (e.getKeyCode()==KeyEvent.VK_S)
        {
            this.hero.setDirect(1);
            this.hero.moveDown();

        }
        else if (e.getKeyCode()==KeyEvent.VK_A)
        {
            this.hero.setDirect(2);
            this.hero.moveLeft();
        }
        else if (e.getKeyCode()== KeyEvent.VK_D)
        {
            this.hero.setDirect(3);
            this.hero.moveRight();
        }
        if (e.getKeyCode()==KeyEvent.VK_J&&this.hero.ss.size()<=4)
        {

            this.hero.shotEnemy();
        }


        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        //每隔100毫秒重画
        while (true)
        {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();

        }
        this.hitEnemy();
        this.hitMe();
        this.repaint();
        }
    }
}

